﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BiffenGUI.BiffenService;

namespace BiffenGUI
{
    public partial class frmMember : Form
    {
        private frmLogin loginForm = null;
        public bool IsGuest { get; set; }
        private BiffenServiceClient service = null;
        public Login CurrentLogin { get; set; }
        private bool kill = false;
        public TabControl TabMenu { get; private set; }

        public frmMember()
        {
            InitializeComponent();
            service = new BiffenServiceClient();
            loginForm = new frmLogin();
            loginForm.SetReferances(service, this);
            loginForm.Owner = this;
            TabMenu = tabMenu;
        }
        
        private void frmMember_Load(object sender, EventArgs e)
        {
            bool friday = service.IsItFriday();

            tabMenu.Controls.Remove(tabBooking);

            memberAddBookingCurrent.SetReferances(this, service);
            memberShowBookings1.SetServiceReferance(this, service);
            searchMovies1.Service = service;
            searchMovies1.BookingFrame = memberAddBookingCurrent;
            searchMovies1.ParentTabControl = tabMenu;
        }

        public void IsEnabled(bool val)
        {
            this.bLogin.Enabled = val;
            this.tabMenu.Enabled = val;
        }

        public void LoginDone(Login l)
        {
            IsEnabled(true);
            if (l != null)
            {
                CurrentLogin = l;
                memberAddBookingCurrent.UpdatePhoneNumber(l.Member.Phone.ToString());
                memberShowBookings1.FillListWithBookings(l.Member.Phone);

                bLogin.Text = "Log ud";

                if (!tabMenu.Controls.Contains(tabBooking))
                {
                    tabMenu.Controls.Add(tabBooking);
                }
            }
        }

        private void bLogin_Click(object sender, EventArgs e)
        {
            if (CurrentLogin != null)
            {
                memberAddBookingCurrent.PrevStep();
                memberAddBookingCurrent.UpdatePhoneNumber("");
            }

            CurrentLogin = null;
            tabMenu.Controls.Remove(tabBooking);
            bLogin.Text = "Log ind";

            loginForm.Show();
            loginForm.ClearFields();
            IsEnabled(false);
        }

        private void frmMember_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!kill)
            {
                e.Cancel = true;
                bLogin_Click(this, e);
            }
        }

        private void bKill_Click(object sender, EventArgs e)
        {
            KillForm();
        }

        public void KillForm()
        {
            kill = true;
            this.Close();
        }

        private void tabMenu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabMenu.SelectedIndex == 2)
            {
                memberShowBookings1.FillListWithBookings(CurrentLogin.Member.Phone);
            }
        }

    }
}
